#!/usr/bin/perl
use Getopt::Std;
getopt("s:r:n:m:o", \%args);
$sample = $args{s};
chomp $sample;
$reference = $args{r};
chomp $reference;
$newreference = $args{n};
chomp $newreference;
$marker = $args{m};
chomp $marker;
$newmarker = $args{o};
chomp $newmarker;
if($sample eq "")
{
        die "entered samplefile name  is empty\n";
}
if($reference eq "")
{
        die "entered reference file name  is empty\n";
}
if($newreference eq "")
{
        die "entered newreference file name is empty\n";
}
if($marker eq "")
{
        die "entered marker file name is empty\n";
}
if($newmarker eq "")
{
        die "entered newmarker file name is empty\n";
}
open(SAM,$sample);
open(BUFF,$reference);

open(MBUFF,$marker);
open(MWRBUFF,">$newmarker");
$line = <BUFF>;
print WRBUFF $line;
open(WRBUFF,">$newreference");
#print WRBUFF $line;
$line = <SAM>;
$line = <SAM>;
while($line = <SAM>)
{
	chomp $line;
	$line =~ s/\n//g;
	$line =~ s/\r//g;
	@array_sam = split(/\s/,$line);
	shift(@array_sam);
	$rsid_sam = shift(@array_sam);
	@array_sam = sort(@array_sam);
	$line = join("",@array_sam);
	$snp = "";
	if($line =~ m/A/)
	{
		$snp = $snp."A";
	}
	if($line =~ m/C/)
        {
                $snp = $snp."C";
        }
	if($line =~ m/G/)
        {
                $snp = $snp."G";
        }
	if($line =~ m/T/)
        {
                $snp = $snp."T";
        }
	@ar_sam =split(//,$snp);
#	print "@ar_sam\n";
	chomp $hash_sam{$rsid_sam};
	#print "$rsid_sam\n";
	$hash_sam{$rsid_sam} =1;
	if($ar_sam[0] ne "")
	{
		$hash_sam0{$rsid_sam} = $ar_sam[0];
	}
	if($ar_sam[1] ne "")
	{
		$hash_sam1{$rsid_sam} = $ar_sam[1];
	}
	#print "$rsid_sam\t$hash_sam{$rsid_sam}\t$hash_sam0{$rsid_sam}\t$hash_sam1{$rsid_sam}\n";
}
while($line = <BUFF>)
{
	chomp $line;
	$mline = <MBUFF>;
	chomp $mline;
	@array = split(/\s/,$line);
	shift(@array);
        $rsid_sam = shift(@array);
	chomp $rsid_sam;
#	print $hash_sam{$rsid_sam}."\t".$rsid_sam."\n";
	if($hash_sam{$rsid_sam} ==1)
	{
	  	if(exists $hash_sam1{$rsid_sam} && exists $hash_sam0{$rsid_sam})
		{	if($mline =~ m/$hash_sam0{$rsid_sam}/ && $mline =~ m/$hash_sam1{$rsid_sam}/)
			{
			}
			else
			{
		#		print "naresh1 $rsid_sam $mline\n";	
				$line =~ tr/ATGC/TACG/;
				$mline =~ tr/ATGC/TACG/;
			}
	 	}
		elsif(exists $hash_sam1{$rsid_sam})
		{
			if($mline =~ m/$hash_sam1{$rsid_sam}/)
                        {
                        }
                        else
                        {
         #               print "naresh2 $rsid_sam\n";
                                $line =~ tr/ATGC/TACG/;
				$mline =~ tr/ATGC/TACG/;
                        }

		}
		else
		{
			if($mline =~ m/$hash_sam0{$rsid_sam}/ )
                        {
                        }
                        else
                        {
          #              print "naresh3 $rsid_sam\n";
                                $line =~ tr/ATGC/TACG/;
				$mline=~ tr/ATGC/TACG/;
                        }


		}
		#print WRBUFF $line."\n";
		#print MWRBUFF $mline."\n";
	}
	print WRBUFF $line."\n";
	print MWRBUFF $mline."\n";

}
